/******************************************************************************
 * Copyright 2023 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/canbus_vehicle/x3epro/protocol/vcu_powerstatus_214.h"
#include "glog/logging.h"
#include "modules/drivers/canbus/common/byte.h"
#include "modules/drivers/canbus/common/canbus_consts.h"

namespace apollo {
namespace canbus {
namespace x3epro {

using ::apollo::drivers::canbus::Byte;

Vcupowerstatus214::Vcupowerstatus214() {}
const int32_t Vcupowerstatus214::ID = 0x214;

void Vcupowerstatus214::Parse(const std::uint8_t* bytes, int32_t length,
                         X3epro* chassis) const {
  chassis->mutable_vcu_powerstatus_214()->set_vcu_powermode(vcu_powermode(bytes, length));
  chassis->mutable_vcu_powerstatus_214()->set_vcu_powermodevalid(vcu_powermodevalid(bytes, length));
  chassis->mutable_vcu_powerstatus_214()->set_replacebatterystateindication(replacebatterystateindication(bytes, length));
  chassis->mutable_vcu_powerstatus_214()->set_bcu_chargedischargecurrent(bcu_chargedischargecurrent(bytes, length));
  chassis->mutable_vcu_powerstatus_214()->set_bcu_batt_internalvoltage(bcu_batt_internalvoltage(bytes, length));
  chassis->mutable_vcu_powerstatus_214()->set_vcu_driverinfo_alivecounter(vcu_driverinfo_alivecounter(bytes, length));
  chassis->mutable_vcu_powerstatus_214()->set_vcu_driverinfo_checksum(vcu_driverinfo_checksum(bytes, length));
}

// config detail: {'bit': 11, 'enum': {0: 'VCU_POWERMODE_OFF', 1: 'VCU_POWERMODE_IG_ON', 2: 'VCU_POWERMODE_POWER_ON', 3: 'VCU_POWERMODE_AUTO_ON', 4-7: 'VCU_POWERMODE_RESERVED'}, 'is_signed_var': False, 'len': 3, 'name': 'vcu_powermode', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Vcu_powerstatus_214::Vcu_powermodeType Vcupowerstatus214::vcu_powermode(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(1, 3);

  Vcu_powerstatus_214::Vcu_powermodeType ret =  static_cast<Vcu_powerstatus_214::Vcu_powermodeType>(x);
  return ret;
}

// config detail: {'bit': 13, 'enum': {0: 'VCU_POWERMODEVALID_NOT_AVAILABLE', 1: 'VCU_POWERMODEVALID_INVALID', 2: 'VCU_POWERMODEVALID_VALID', 3: 'VCU_POWERMODEVALID_RESERVED'}, 'is_signed_var': False, 'len': 2, 'name': 'vcu_powermodevalid', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Vcu_powerstatus_214::Vcu_powermodevalidType Vcupowerstatus214::vcu_powermodevalid(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(4, 2);

  Vcu_powerstatus_214::Vcu_powermodevalidType ret =  static_cast<Vcu_powerstatus_214::Vcu_powermodevalidType>(x);
  return ret;
}

// config detail: {'bit': 14, 'enum': {0: 'REPLACEBATTERYSTATEINDICATION_NOTACTIVATE', 1: 'REPLACEBATTERYSTATEINDICATION_ACTIVATE'}, 'is_signed_var': False, 'len': 1, 'name': 'replacebatterystateindication', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Vcu_powerstatus_214::ReplacebatterystateindicationType Vcupowerstatus214::replacebatterystateindication(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(6, 1);

  Vcu_powerstatus_214::ReplacebatterystateindicationType ret =  static_cast<Vcu_powerstatus_214::ReplacebatterystateindicationType>(x);
  return ret;
}

// config detail: {'bit': 23, 'is_signed_var': False, 'len': 16, 'name': 'bcu_chargedischargecurrent', 'offset': -400.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'A', 'precision': 0.02, 'type': 'double'}
double Vcupowerstatus214::bcu_chargedischargecurrent(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 2);
  int32_t x = t0.get_byte(0, 8);

  Byte t1(bytes + 3);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.020000 + -400.000000;
  return ret;
}

// config detail: {'bit': 39, 'is_signed_var': False, 'len': 16, 'name': 'bcu_batt_internalvoltage', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'V', 'precision': 0.01, 'type': 'double'}
double Vcupowerstatus214::bcu_batt_internalvoltage(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 4);
  int32_t x = t0.get_byte(0, 8);

  Byte t1(bytes + 5);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.010000;
  return ret;
}

// config detail: {'bit': 55, 'is_signed_var': False, 'len': 4, 'name': 'vcu_driverinfo_alivecounter', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'int'}
int Vcupowerstatus214::vcu_driverinfo_alivecounter(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(4, 4);

  int ret = x;
  return ret;
}

// config detail: {'bit': 63, 'is_signed_var': False, 'len': 8, 'name': 'vcu_driverinfo_checksum', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'int'}
int Vcupowerstatus214::vcu_driverinfo_checksum(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 7);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}
}  // namespace x3epro
}  // namespace canbus
}  // namespace apollo
